import smtplib
from email.mime.text import MIMEText
from email.header import Header

from config import Config


class EmailNotifier:

    def __init__(self, cfg: Config):
        self._sender_smtp_server = cfg.sender_smtp_server
        self._sender_smtp_user = cfg.sender_smtp_user
        self._sender_smtp_pass = cfg.sender_smtp_pass
        self._sender_email = cfg.sender_email
        self._receiver_emails = cfg.receiver_emails

    def send(self, subject: str, content: str):
        server = smtplib.SMTP_SSL(self._sender_smtp_server)
        server.login(
            user=self._sender_smtp_user,
            password=self._sender_smtp_pass
        )

        enc = 'utf-8'
        msg = MIMEText(content, 'plain', enc)
        msg['From'] = Header(self._sender_email, enc)
        msg['To'] = Header('', enc)
        msg['Subject'] = Header(subject, enc)

        server.sendmail(
            from_addr=self._sender_email,
            to_addrs=self._receiver_emails,
            msg=msg.as_string())
